package com.sky.admin.service.impl;

import com.sky.admin.domain.vo.BusinessDataVO;
import com.sky.admin.service.WorkspaceService;
import com.sky.api.client.ProductFeignClient;
import com.sky.api.client.TradeFeignClient;
import com.sky.api.client.UserFeignClient;
import com.sky.api.domain.entity.Orders;
import com.sky.api.domain.vo.DishOverViewVO;
import com.sky.api.domain.vo.OrderOverViewVO;
import com.sky.api.domain.vo.SetmealOverViewVO;
import com.sky.common.constant.StatusConstant;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @author toki
 */
@Service
@Slf4j
@RequiredArgsConstructor
//@EnableFeignClients(clients = {TradeFeignClient.class,
//        UserFeignClient.class, ProductFeignClient.class})
public class WorkspaceServiceImpl implements WorkspaceService {

    private final TradeFeignClient tradeFeignClient;
    private final UserFeignClient userFeignClient;
    private final ProductFeignClient productFeignClient;

    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        log.info("工作台统计当天营业数据 begin:{}, end:{}", begin, end);

        // 当天订单
        Integer totalOrderCount = tradeFeignClient.countByMap(begin, end, null).getData();

        // 营业额
        Double turnover = tradeFeignClient.sumByMap(begin, end, Orders.COMPLETED).getData();
        turnover = turnover == null ? 0.0 : turnover;

        // 有效订单
        Integer validOrderCount = tradeFeignClient.countByMap(begin, end, Orders.COMPLETED).getData();

        // 订单完成率
        double orderCompletionRate = 0.0;
        if (totalOrderCount != 0) {
            orderCompletionRate = (double) validOrderCount / totalOrderCount;
        }
        // 平均客单价
        double unitPrice = 0.0;
        if (validOrderCount != 0) {
            unitPrice = turnover / validOrderCount;
        }
        // 新增用户
        Integer newUsers = userFeignClient.countByMap(begin, end).getData();
        return BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();
    }

    @Override
    public OrderOverViewVO getOrderOverView() {
        log.info("工作台订单概览");
        // 今日订单
        final LocalDateTime begin = LocalDateTime.now().with(LocalTime.MIN);
        //待接单
        Integer waitingOrders = tradeFeignClient.countByMap(begin, null, Orders.TO_BE_CONFIRMED).getData();
        //待派送
        Integer deliveredOrders = tradeFeignClient.countByMap(begin, null, Orders.CONFIRMED).getData();
        //已完成
        Integer completedOrders = tradeFeignClient.countByMap(begin, null, Orders.COMPLETED).getData();
        //已取消
        Integer cancelledOrders = tradeFeignClient.countByMap(begin, null, Orders.CANCELLED).getData();
        //全部订单
        Integer allOrders = tradeFeignClient.countByMap(begin, null, null).getData();
        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();
    }

    @Override
    public DishOverViewVO getDishOverView() {
        log.info("工作台菜品概览");
        // 起售数量
        Integer sold = productFeignClient.countDishByMap(StatusConstant.ENABLE).getData();
        // 停售数量
        Integer discontinued = productFeignClient.countDishByMap(StatusConstant.DISABLE).getData();
        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    @Override
    public SetmealOverViewVO getSetmealOverView() {
        log.info("工作台套餐概览");
        // 起售数量
        Integer sold = productFeignClient.countSetmealByMap(StatusConstant.ENABLE).getData();
        // 停售数量
        Integer discontinued = productFeignClient.countSetmealByMap(StatusConstant.DISABLE).getData();
        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }
}
